<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="name">
    <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>

  <button @click="ls()">LoacalStroage</button>
  <button @click="ss()">sessionStroge</button>
</el-form>
</template>


<script>
  export default {
    data() {  // 存放数据
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }else{
          callback()
        }
      };
      var checkName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        }else{
          callback()
        }
      };
      return {
        ruleForm: {
          name:'',
          pass: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ], 
          name: [
            { validator: checkName, trigger: 'blur' }
          ]
        }
      };
    },

    methods: {
      submitForm(formName) {
        // console.log(formName)
        // axios.post('http://127.0.0.1:8888/api/private/v1/login',{
        //   username:formName.name,
        //   password:formName.pass
        // }).then(function(res){
        //   console.log(res)
        // })
        
        // this.$http.post('login',{
        //   username:formName.name,
        //   password:formName.pass
        // }).then(function(res){
        //   console.log(res)
        // })
        // console.log(formName)
        this.$refs[formName].validate((valid) => {
          console.log(this);
          var that = this;
          if (valid) {
            this.$http.post('login',{
              username:this.ruleForm.name,
              password:this.ruleForm.pass
            }).then(function(res){
              console.log(res);
              // 解构赋值，axios会进行2次封装
              var { data } = res;
              if(data.meta.status != 200){
                that.$message.error(data.meta.msg);
              }else{
                that.$message(data.meta.msg);
                // 存储token
                sessionStorage.setItem('jhToken',data.data.token);
                // 路由跳转
                // that.$router.push('user')
                that.$router.push('index')

              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      changeData:function(){
        this.president = '特朗普'
      },
      deleVue(){
        this.$destroy();
      },
      ls(){
        localStorage.setItem('ls','我是永不过期的范冰冰');
      },
      ss(){
        sessionStorage.setItem('ss','我是关闭页面或浏览器就会过期的王宝强');
        alert(localStorage.getItem('ls'));
        localStorage.removeItem('ls')
      }
       
    }
  }
</script>